package com.xujiang.myblog.adapter;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xujiang.myblog.R;
import com.xujiang.myblog.entity.Blog;
import com.xujiang.myblog.tools.ListToString;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by xujiang on 2017/4/12.
 */

public class BlogFragmentRLVAdapter extends BaseQuickAdapter<Blog,BaseViewHolder> {
    private Context context;
    public BlogFragmentRLVAdapter(int layoutResId, List<Blog> list,Context context) {
        super(layoutResId,list);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, Blog item) {
        helper.addOnClickListener(R.id.user).addOnClickListener(R.id.files);
        helper.setText(R.id.time,item.getTime())
                .setText(R.id.name,item.getName())
                .setText(R.id.title,item.getTitle())
                .setText(R.id.content,item.getContent())
                .setText(R.id.tag, ListToString.listtostringnull(item.getTaglist()));
        //((ImageView)helper.getView(R.id.photo));
        Glide.with(context).load(item.getUrl()).bitmapTransform(new CropCircleTransformation(mContext)).into((ImageView)helper.getView(R.id.photo));

        String str = ListToString.listtostringnull(item.getFilesurl());
        SpannableString spannableInfo = new SpannableString(str);
        for(int i=0;i<item.getFilesurl().size();i++){
            String listitem = item.getFilesurl().get(i);
            spannableInfo.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    //此处处理文件下载功能
                }
            },
            str.indexOf(listitem),
            str.indexOf(listitem)+listitem.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
